﻿using System;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;
namespace LitTools
{
    /// <summary>
    /// GM管理类
    /// </summary>
    public partial class GMManager : MonoSingleton<GMManager>
    {
        //方法字典，或者叫指令字典
        private Dictionary<string, CommondInfo> commandDict;

        #region 公有方法
        /// <summary>
        /// 初始化，获取所有标记了指令的类
        /// </summary>
        public override void Init()
        {
            commandDict = new Dictionary<string, CommondInfo>();
            var asm = Assembly.GetExecutingAssembly();
            Type[] types = asm.GetTypes().Where(t => t.IsSubclassOf(typeof(GMClassBase))).ToArray();
            foreach (Type tt in types)
            {
                var methods = tt.GetMethods();
                foreach (var item in methods)
                {
                    var attribute = item.GetCustomAttributes(typeof(GMCommondAttribute), false);
                    if (attribute != null
                        && attribute.Length > 0)
                    {
                        GMCommondAttribute gmc = attribute[0] as GMCommondAttribute;
                        if (commandDict.ContainsKey(gmc.cmd))
                        {
                            string addObj = commandDict[gmc.cmd].callObj.GetType().ToString();
                            string addMethod = commandDict[gmc.cmd].callMethod.Name;
                            Debug.LogError($"same command has been added {addObj}.{addMethod},will add is" +
                                $"{tt}.{item.Name}");
                            continue;
                        }
                        commandDict.Add(gmc.cmd, new CommondInfo()
                        {
                            callObj = Activator.CreateInstance(tt),
                            callMethod = item,
                        });
                    }
                }
            }
        }

        /// <summary>
        /// Call指令
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public string Call(string inputStr)
        {
            var tempStr = inputStr.Split(' ');
            var param = new List<string>();
            for (int i = 0; i < tempStr.Length; i++)
            {
                param.Add(tempStr[i]);
            }
            if (commandDict.ContainsKey(tempStr[0]))
            {
                var obj = commandDict[tempStr[0]].callObj;
                var method = commandDict[tempStr[0]].callMethod;
                var info = method.GetCustomAttributes(typeof(GMCommondAttribute), false)[0] as GMCommondAttribute;
                string result = method.Invoke(obj, new object[] { param.ToArray() }) as string;
                result += $"\n desc:{info.desc}";
                Debug.Log("GMCommond: " + result);
                return result;
            }
            else
            {
                return "command not find";
            }
        }

        /// <summary>
        /// 启动GM
        /// </summary>
        public void Invoke()
        {
            Debug.Log("GM启动");
        }
        #endregion

        /// <summary>
        /// 指令信息类
        /// </summary>
        public class CommondInfo
        {
            /// <summary>
            /// 调用的类
            /// </summary>
            public object callObj;
            /// <summary>
            /// 调用的方法
            /// </summary>
            public MethodInfo callMethod;
        }
    }
}